# -*- coding:utf-8 -*-

import requests
import json


def getPM25():
    url = "http://www.pm25.in/api/querys/pm2_5.json"

    querystring = {"city": "zhuhai", "token": "5j1znBVAsnSf5xQyNQyq"}

    payload = ""
    headers = {
        'cache-control': "no-cache",
        'Postman-Token': "51fe72b9-439d-4c35-9f35-6ed8504afb89"
    }

    response = requests.request("GET", url, data=payload, headers=headers, params=querystring)
    print(response.text)


def getWeatherInfo(city):
    url = "https://api.seniverse.com/v3/weather/now.json"

    querystring = {"key": "62hlk15vo3r32w6e", "location": city, "language": "zh-Hans", "unit": "c"}

    payload = ""
    headers = {
        'cache-control': "no-cache",
        'Postman-Token': "f6fa6403-3079-4da8-bfe9-17c7a094d37f"
    }
    response = requests.request("GET", url, data=payload, headers=headers, params=querystring)
    result = json.loads(response.text)["results"][0]
    condition = result['now']['text']
    temp_now = result['now']['temperature']

    url = "https://api.seniverse.com/v3/weather/daily.json"
    querystring = {"key": "62hlk15vo3r32w6e", "location": "beijing", "language": "zh-Hans", "unit": "c", "start": "0",
                   "days": "3"}

    response = requests.request("GET", url, data=payload, headers=headers, params=querystring)
    result = json.loads(response.text)["results"][0]
    location = result['location']['name']
    now = result['daily'][0]
    return u"{location} {date} :现在{temp_now}度,{condition}, 最高温{high}度 最低温{low}度".format(location=location,
                                                                                        date=now['date'],
                                                                                        high=now['high'],
                                                                                        low=now['low'],
                                                                                        temp_now=temp_now,
                                                                                        condition=condition)
    # for perday in result['daily']:
    #     yield u"{location} {date} : 最高温{high}度 最低温{low}度".format(location=location, date=perday['date'],
    #                                                              high=perday['high'], low=perday['low'])


if __name__ == '__main__':
    print(getWeatherInfo("北京"))
